package com.zx.pm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

import com.zx.pm.domain.PurchaseProject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.enums.BusinessType;
import com.zx.pm.service.IPurchaseProjectService;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.common.core.page.TableDataInfo;

/**
 * 采购项目信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-06
 */
@RestController
@RequestMapping("/pm/purchase")
public class PurchaseProjectController extends BaseController
{
    @Autowired
    private IPurchaseProjectService purchaseProjectService;

    /**
     * 查询采购项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(PurchaseProject purchaseProject)
    {
        startPage();
        List<PurchaseProject> list = purchaseProjectService.selectPurchaseProjectList(purchaseProject);
        return getDataTable(list);
    }

    /**
     * 导出采购项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:export')")
    @Log(title = "采购项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseProject purchaseProject)
    {
        List<PurchaseProject> list = purchaseProjectService.selectPurchaseProjectList(purchaseProject);
        ExcelUtil<PurchaseProject> util = new ExcelUtil<PurchaseProject>(PurchaseProject.class);
        util.exportExcel(response, list, "采购项目信息数据");
    }

    /**
     * 导入采购项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:import')")
    @Log(title = "采购项目信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<PurchaseProject> util = new ExcelUtil<PurchaseProject>(PurchaseProject.class);
        List<PurchaseProject> list = util.importExcel(file.getInputStream());
        int inserted = 0;
        for (PurchaseProject item : list)
        {
            inserted += purchaseProjectService.insertPurchaseProject(item);
        }
        return success("导入成功，新增" + inserted + "条");
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<PurchaseProject> util = new ExcelUtil<PurchaseProject>(PurchaseProject.class);
        util.importTemplateExcel(response, "采购项目信息数据");
    }

    /**
     * 获取采购项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(purchaseProjectService.selectPurchaseProjectById(id));
    }

    /**
     * 新增采购项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:add')")
    @Log(title = "采购项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseProject purchaseProject)
    {
        return toAjax(purchaseProjectService.insertPurchaseProject(purchaseProject));
    }

    /**
     * 修改采购项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:edit')")
    @Log(title = "采购项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PurchaseProject purchaseProject)
    {
        return toAjax(purchaseProjectService.updatePurchaseProject(purchaseProject));
    }

    /**
     * 删除采购项目信息
     */
    @PreAuthorize("@ss.hasPermi('pm:purchase:remove')")
    @Log(title = "采购项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(purchaseProjectService.deletePurchaseProjectByIds(ids));
    }
}
